package com.panshi.config;
import com.alibaba.druid.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImgUtls {

    public static String getimgurl(String html) {
        //获取对象中的第一个img的src内容
        String img = "";
        String imgStr = null;
        boolean bool = true;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        if(!StringUtils.isEmpty(html)){
            Matcher m_image = p_image.matcher(html);
            while (m_image.find() && bool==true) {
                // 得到<img />数据
                img = m_image.group();
                // 匹配<img>中的src数据
                Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                while (m.find()) {
                    imgStr = m.group(1);
                    bool = false;
                    break;
                }
            }
        }
        return imgStr;
    }


}
